using UnityEngine;

[ExecuteInEditMode]
public class ControllerWall : ControllerGeneric
{
	public float height = 1f;

	public float width = 1f;

	public float depth = 1f;

	public GameObject cube;

	private bool staticState;

	public override void OnLevelInPlace(Transform levelTransform)
	{
		base.OnLevelInPlace(levelTransform);
		if (levelTransform == base.transform.root)
		{
			PrepareForGame();
		}
	}

	public void PrepareForGame()
	{
		SetNames();
		SetParenting();
		base.name = "will_destroy_" + base.name;
		if (Application.isPlaying)
		{
			Object.Destroy(base.gameObject);
		}
		else
		{
			Object.DestroyImmediate(base.gameObject);
		}
	}

	private void Update()
	{
		if (!Application.isEditor || Application.isPlaying)
		{
			return;
		}
		SetupRemoteControl();
		if (IsRemoteControlActive())
		{
			base.transform.position = remoteControl.transform.position;
			base.transform.rotation = remoteControl.transform.rotation;
			base.transform.localScale = remoteControl.transform.localScale;
			height = remoteControl.transform.localScale.y;
			width = remoteControl.transform.localScale.x;
			depth = remoteControl.transform.localScale.z;
		}
		if (height < 0.0001f)
		{
			height = 0.0001f;
		}
		if (width < 0.0001f)
		{
			width = 0.0001f;
		}
		if (depth < 0.0001f)
		{
			depth = 0.0001f;
		}
		if (remoteControl != null)
		{
			if (remoteControl.manualScale)
			{
				remoteControl.scale = new Vector3(remoteControl.scale.x, remoteControl.scale.y, remoteControl.scale.z);
			}
			else
			{
				remoteControl.transform.localScale = new Vector3(width, height, depth);
			}
		}
		Setup();
		SetupUVs();
	}

	private void Setup()
	{
		AssureParenting();
	}

	private void SetStatic(bool newStaticState = true)
	{
		if (newStaticState != staticState)
		{
			staticState = newStaticState;
			cube.isStatic = staticState;
		}
	}

	private void SetParenting()
	{
		for (int childCount = base.transform.childCount; childCount > 0; childCount = base.transform.childCount)
		{
			base.transform.GetChild(0).parent = base.transform.parent;
		}
	}

	private void SetNames()
	{
		cube.name = "Wall";
	}

	public void SetupRemoteControl()
	{
		if (remoteControl == null)
		{
			InitRemoteControl();
			remoteControl.gameObject.name = remoteControl.gameObject.name + base.name;
			remoteControl.gameObject.transform.position = base.transform.position;
			remoteControl.gameObject.transform.rotation = base.transform.rotation;
			remoteControl.gameObject.transform.localScale = new Vector3(width, height, depth);
		}
	}

	public override void CopyFromRemoteControl(RemoteControl rc)
	{
		base.CopyFromRemoteControl(rc);
		CopyParentingFromRemoteControl(rc);
	}

	public void SetupUVs()
	{
		Mesh mesh = cube.GetComponent<MeshFilter>().mesh;
		Vector2[] uv = mesh.uv;
		Vector3[] normals = mesh.normals;
		Vector3[] vertices = mesh.vertices;
		float num = 2.5f;
		float num2 = 2.5f;
		float num3 = 1f;
		for (int i = 0; i < vertices.Length; i++)
		{
			Vector3 normalized = normals[i].normalized;
			Vector3 vector = vertices[i];
			if (normalized == Vector3.left || normalized == Vector3.right)
			{
				float num4 = Mathf.Sign(vector.z);
				num3 = Mathf.Sign(vector.y);
				uv[i].x = 0.5f + num4 * num * depth / 2f;
				uv[i].y = 0.5f + num3 * num2 * height / 2f;
			}
			else if (normalized == Vector3.forward || normalized == Vector3.back)
			{
				float num4 = Mathf.Sign(vector.x);
				num3 = Mathf.Sign(vector.y);
				uv[i].x = 0.5f + num4 * num * width / 2f;
				uv[i].y = 0.5f + num3 * num2 * height / 2f;
			}
			else if (normalized == Vector3.up || normalized == Vector3.down)
			{
				float num4 = Mathf.Sign(vector.x);
				num3 = Mathf.Sign(vector.z);
				uv[i].x = 0.5f + num4 * num * width / 2f;
				uv[i].y = 0.5f + num3 * num2 * depth / 2f;
			}
		}
		mesh.uv = uv;
	}
}
